
/*
 * rss.reader 1.0 (2011-06-10)
 *
 * Copyright (c) 2011 Jack Tien (http://www.openfire.tk/lemon)
 *
 *
 * Built upon jQuery 1.4.1 (http://jquery.com)
 * 
 */

var rss={};
;rss.reader = {
	feedName:"",
	rssUrl:"",
	portletId:"",
	setup:function(portletId){
		rss.reader.portletId = portletId;
		$('#'+ portletId +' .AddRSS').click(function(){
			rss.reader.getRSSHTML('Add');
		});
		$('#'+ portletId +' .GetRSS').click(function(){
			rss.reader.getRSSHTML('Get');
		});
	},
	check:function(){
		$('#'+ rss.reader.portletId +' .rssStatus').empty();
		inputUrl = $('#'+ rss.reader.portletId +' .rssUrl').val();
	    rss.reader.rssUrl = encodeURI("goal/rssReader!getRSSXML.action?rssUrl="+inputUrl);
	    if(inputUrl == ''){
	        $('#'+ rss.reader.portletId +' .rssStatus').text('Please input a rss url!');
	        return false;
	    }else{
	    	return true;
	    }
	},
	getRSSHTML:function(type){
		if(rss.reader.check()){
			 $.getFeed({
		         url : rss.reader.rssUrl,
		         success: function(feed) {
		            if(feed==''){
		                $('#'+ rss.reader.portletId +' .rssStatus').text('Please check input url, url not found or the server is unavaialbe!');
		                return;
		            }
		            rss.reader.parseFeedToHtml(feed);
		            if(type=='Add'){
		                var rssList ='<div><a href="'+ rss.reader.rssUrl +'">'+ rss.reader.feedName +'</a></div>';
		                $('#'+ rss.reader.portletId +' .rssList').append($(rssList));
		            }
		        }
		    });
		}
	},
	parseFeedToHtml:function(feed){
		rss.reader.feedName = feed.title;
	    if(rss.reader.feedName == ''){
	    	 $('#'+ rss.reader.portletId +' .rssStatus').text('Please check input url, url not found or the server is unavaialbe!')
	    	 return;
	    }
	    var feedTitle = '<div id="feedTitle">'+
	                           '<div id="feedTitleContainer">'+
	                            '<h1 id="feedTitleText">'+ feed.title +'</h1>'+
	                            '<h2 id="feedSubtitleText">'+ feed.description+'</h2>'+
	                           '</div>'+
	                      '</div>';
	     $('#'+ rss.reader.portletId +' #feedTitle').replaceWith($(feedTitle));
	    
	     var feedEntry;         
	     $('#'+ rss.reader.portletId +' #feedContent').empty();
	     for(var i=0;i<feed.items.length;i++){
	         var feedItem = feed.items[i];
	         feedEntry =    '<div class="entry">'+
	                        '<h3>'+
	                            '<a target="_blank" href="'+feedItem.link+'">'+feedItem.title+'</a>'+
	                            '<div class="lastUpdated">'+feedItem.updated+'</div>'+
	                        '</h3>'+
	                    '<div base="'+feedItem.base+'" class="feedEntryContent">'+feedItem.description+'</div>'+
	                    '</div>'+
	                    '<div style="clear: both;"></div>';   
	         $('#'+ rss.reader.portletId +' #feedContent').append(feedEntry);
	      }
	}
	
	
};
